package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
import cn.com.ghy.android.util.XMLStatic;

public class OrderReportInfoBeanParser {

	public static String createXMLOrderReportInfoQuery(OrderReportInfoBean orderReportInfo,
			Writer writer) throws Exception {
		XmlSerializer xmlSerializer = Xml.newSerializer();

		xmlSerializer.setOutput(writer);
		xmlSerializer.startDocument("UTF-8", true);
		xmlSerializer.startTag("", XMLStatic.CONTENT);
		xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
		xmlSerializer.startTag("", XMLStatic.RECORDS);

		xmlSerializer.startTag("", XMLStatic.ORDERID);
		xmlSerializer.text(orderReportInfo.getOrderid());
		xmlSerializer.endTag("", XMLStatic.ORDERID);
		xmlSerializer.startTag("", XMLStatic.ORDERNO);
		xmlSerializer.text(orderReportInfo.getOrderno());
		xmlSerializer.endTag("", XMLStatic.ORDERNO);
		xmlSerializer.startTag("", XMLStatic.PARTNER);
		xmlSerializer.text(orderReportInfo.getPartner());
		xmlSerializer.endTag("", XMLStatic.PARTNER);
		xmlSerializer.startTag("", XMLStatic.CUSTNAME);
		xmlSerializer.text(orderReportInfo.getCustname());
		xmlSerializer.endTag("", XMLStatic.CUSTNAME);
		xmlSerializer.startTag("", XMLStatic.CRBEGINDATE);
		xmlSerializer.text(orderReportInfo.getCrbegindate());
		xmlSerializer.endTag("", XMLStatic.CRBEGINDATE);
		xmlSerializer.startTag("", XMLStatic.CRENDDATE);
		xmlSerializer.text(orderReportInfo.getCrenddate());
		xmlSerializer.endTag("", XMLStatic.CRENDDATE);
		xmlSerializer.startTag("", XMLStatic.ORDERSTATE);
		xmlSerializer.text(orderReportInfo.getOrderstate().getOrderStatusId());
		xmlSerializer.endTag("", XMLStatic.ORDERSTATE);

		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace(
				"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}

	public static List<OrderReportInfoBean> parseXMLForOrderReportInfo(InputStream in)
			throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		OrderReportInfoBean orderReportInfoBean = null;
		List<OrderReportInfoBean> list = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<OrderReportInfoBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("Table".equals(parser.getName())) {
						orderReportInfoBean = new OrderReportInfoBean();
					} else if (null != orderReportInfoBean) {
						if ("ORDERID".equals(parser.getName())) {
							orderReportInfoBean.setOrderid(parser.nextText());
						} else if ("ORDERNO".equals(parser.getName())) {
							orderReportInfoBean.setOrderno(parser.nextText());
						} else if ("CUSTNAME".equals(parser.getName())) {
							orderReportInfoBean.setCustname(parser.nextText());
						} else if ("PERNR".equals(parser.getName())) {
							orderReportInfoBean.setPernr(parser.nextText());
						} else if ("PARTNER".equals(parser.getName())) {
							orderReportInfoBean.setPartner(parser.nextText());
						} else if ("TOTALNUM".equals(parser.getName())) {
							orderReportInfoBean.setTotalnum(parser.nextText());
						} else if ("TOTALMONEY".equals(parser.getName())) {
							orderReportInfoBean.setTotalmoney(parser.nextText());
						} else if ("TOTALSENDNUM".equals(parser.getName())) {
							orderReportInfoBean.setTotalsendnum(parser.nextText());
						} else if ("TOTALSENDMONEY".equals(parser.getName())) {
							orderReportInfoBean.setTotalsendmoney(parser.nextText());
						} else if ("CUSERTYPE".equals(parser.getName())) {
							orderReportInfoBean.setCusertype(parser.nextText());
						} else if ("ISPASS".equals(parser.getName())) {
							orderReportInfoBean.setIspass(parser.nextText());
						} else if ("ISSEND".equals(parser.getName())) {
							orderReportInfoBean.setIssend(parser.nextText());
						} else if ("ORDERTYPE".equals(parser.getName())) {
							orderReportInfoBean.setOrdertype(parser.nextText());
						} else if ("ISPASSNAME".equals(parser.getName())) {
							orderReportInfoBean.setIspassname(parser.nextText());
						} else if ("ISSENDNAME".equals(parser.getName())) {
							orderReportInfoBean.setIssendname(parser.nextText());
						} else if ("ORDERZT".equals(parser.getName())) {
							orderReportInfoBean.setOrderzt(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("Table".equals(parser.getName())) {
						if (null != orderReportInfoBean) {
							list.add(orderReportInfoBean);
							orderReportInfoBean = null;
						}
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}

	public static CustomerInfoBean parseXMLForCustomerInfoBean(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		CustomerInfoBean customerInfoBean = null;
		OrderReportInfoBean orderReportInfoBean = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					break;

				case XmlPullParser.START_TAG :
					if ("orderhead".equals(parser.getName())) {
						customerInfoBean = new CustomerInfoBean();
						orderReportInfoBean = new OrderReportInfoBean();
					} else if (null != customerInfoBean && null != orderReportInfoBean) {
						if ("ORDERID".equals(parser.getName())) {
							orderReportInfoBean.setOrderid(parser.nextText());
						} else if ("ORDERNO".equals(parser.getName())) {
							orderReportInfoBean.setOrderno(parser.nextText());
						} else if ("CUSTNAME".equals(parser.getName())) {
							customerInfoBean.setName_org1(parser.nextText());
						} else if ("PARTNER".equals(parser.getName())) {
							customerInfoBean.setPartner(parser.nextText());
						} else if ("LXR".equals(parser.getName())) {
							customerInfoBean.setLxr(parser.nextText());
						} else if ("TEL".equals(parser.getName())) {
							customerInfoBean.setLxdh(parser.nextText());
						} else if ("ADDRESS".equals(parser.getName())) {
							customerInfoBean.setStreet(parser.nextText());
						} else if ("JXS".equals(parser.getName())) {
							customerInfoBean.setJxsid(parser.nextText());
						} else if ("JXSNAME".equals(parser.getName())) {
							customerInfoBean.setJxsname(parser.nextText());
						} else if ("FXSID".equals(parser.getName())) {
							customerInfoBean.setFxsid(parser.nextText());
						} else if ("FXSNAME".equals(parser.getName())) {
							customerInfoBean.setFxsname(parser.nextText());
						} else if ("CREATETIME".equals(parser.getName())) {
							orderReportInfoBean.setCreatetime(parser.nextText());
						} else if ("XQRIQI".equals(parser.getName())) {
							orderReportInfoBean.setXqriqi(parser.nextText());
						} else if ("TOTALMONEYNORATE".equals(parser.getName())) {
							orderReportInfoBean.setTotalmoneynorate(parser.nextText());
						} else if ("TOTALSENDMONEYNORATE".equals(parser.getName())) {
							orderReportInfoBean.setTotalsendmoneynorate(parser.nextText());
						} else if ("TOTALSENDNUM".equals(parser.getName())) {
							orderReportInfoBean.setTotalsendnum(parser.nextText());
						} else if ("TOTALSENDMONEY".equals(parser.getName())) {
							orderReportInfoBean.setTotalsendmoney(parser.nextText());
						} else if ("TOTALNUM".equals(parser.getName())) {
							orderReportInfoBean.setTotalmoney(parser.nextText());
						} else if ("TOTALMONEY".equals(parser.getName())) {
							orderReportInfoBean.setTotalmoney(parser.nextText());
						} else if ("FAX".equals(parser.getName())) {
							customerInfoBean.setFax(parser.nextText());
						} else if ("TOSEND".equals(parser.getName())) {
							customerInfoBean.setTosend(parser.nextText());
						} else if ("FAXSTATE".equals(parser.getName())) {
							customerInfoBean.setFaxstate(parser.nextText());
						}
						
						if ("ISCAR".equals(parser.getName())) {
							customerInfoBean.setIsCar(parser.nextText());
						}
						
						
					}
					break;
				case XmlPullParser.END_TAG :
					if ("orderhead".equals(parser.getName())) {
						if (null != customerInfoBean && null != orderReportInfoBean) {
							customerInfoBean.setOrderReportInfoBean(orderReportInfoBean);
							orderReportInfoBean = null;
						}
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return customerInfoBean;
	}

	public static List<OrderDetailBean> parseXMLForOrderDetailBean(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		OrderDetailBean orderDetail = null;
		UnitInfoBean unitInfo = null;
		List<OrderDetailBean> list = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<OrderDetailBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("orderdetail".equals(parser.getName())) {
						orderDetail = new OrderDetailBean();
						unitInfo = new UnitInfoBean();
					} else if (null != orderDetail) {
						if ("ORDERID".equals(parser.getName())) {
							orderDetail.setOrderid(parser.nextText());
						} else if ("BISMT".equals(parser.getName())) {
							orderDetail.setBismt(parser.nextText());
						} else if ("MAKTX".equals(parser.getName())) {
							orderDetail.setMaktx(parser.nextText());
						} else if ("SHOP_CODE".equals(parser.getName())) {
							orderDetail.setShop_code(parser.nextText());
						} else if ("UNIT".equals(parser.getName())) {
							unitInfo.setUnit(parser.nextText());
						} else if ("UNITNAME".equals(parser.getName())) {
							unitInfo.setUnitname(parser.nextText());
						} else if ("NUM".equals(parser.getName())) {
							orderDetail.setNum(parser.nextText());
						} else if ("ORDERPRICE".equals(parser.getName())) {
							orderDetail.setOrderprice(parser.nextText());
						} else if ("TOTALPRICE".equals(parser.getName())) {
							orderDetail.setTotalprice(parser.nextText());
						} else if ("REMARK".equals(parser.getName())) {
							orderDetail.setRemark(parser.nextText());
						} else if ("MATNR".equals(parser.getName())) {
							orderDetail.setMatnr(parser.nextText());
						} else if ("SENDNUM".equals(parser.getName())) {
							orderDetail.setSendnum(parser.nextText());
						} else if ("SENDPRICE".equals(parser.getName())) {
							orderDetail.setSendprice(parser.nextText());
						} else if ("TOTALSENDPRICE".equals(parser.getName())) {
							orderDetail.setTotalsendprice(parser.nextText());
						} else if ("TAG".equals(parser.getName())) {
							orderDetail.setTag(parser.nextText());
						}else if ("SENDUNITNAME".equals(parser.getName())) {
							orderDetail.setSendunitname(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("orderdetail".equals(parser.getName())) {
						if (null != orderDetail && null != unitInfo) {
							orderDetail.setUnitInfo(unitInfo);
							unitInfo = null;
						}
						list.add(orderDetail);
						orderDetail = null;
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}
}
